/**
 * Main类：程序入口
 */
class Main {
    public static s_width: number = 400;
    public static s_height: number = 852;
    //用于分享的图片
    public share_log_img: string = 'http://www.saunter.shop:666/images/sgame/airplane_share_logo.jpg';
    //用于停止动画
    public static is_playing: boolean = true;
    //角色容器
    public role_contain: Laya.Sprite;
    //信息面板
    public game_panel: Gameinfo;
    //背景
    private background: Background;
    //英雄
    private hero: Plane;
    //分数
    public total_score: number = 0;
    //游戏等级
    public game_level: number = 1;
    public wx = null;
    constructor() {
        Laya.MiniAdpter.init();
        Laya.init(Main.s_width, Main.s_height, Laya.WebGL);
        //针对微信的屏幕适配
        if(Laya.Browser.onWeiXin) {
            this.wx = Laya.Browser.window.wx;
            Laya.stage.scaleMode = Laya.Stage.SCALE_FIXED_HEIGHT;
            Laya.stage.alignH = Laya.Stage.ALIGN_CENTER;
            Laya.stage.screenMode = Laya.Stage.SCREEN_VERTICAL;
            this.wx.showShareMenu();
            var self  = this;
            this.wx.onShareAppMessage(function () {
                return {
                    title: '飞机play',
                    imageUrl: self.share_log_img
                }
            })
        }
        
        Laya.loader.load(["res/atlas/war.atlas","res/atlas/menu.atlas"], Laya.Handler.create(this, this.onLoaded), null, Laya.Loader.ATLAS);
    }

    //资源加载完成后回调
    onLoaded(): void {
        Flyrole.cachedAni();
        this.background = new Background();
        Laya.stage.addChild(this.background);
        
        this.role_contain = new Laya.Sprite();
        Laya.stage.addChild(this.role_contain)

        this.game_panel = new Gameinfo();
        Laya.stage.addChild(this.game_panel);

        this.onStart();
    }

    //主要用于重启游戏
    onStart(): void {
        if(this.role_contain.numChildren) {
            this.role_contain.removeChildren();
            this.addGameLavel(1-this.game_level);
        }else{
            this.addGameLavel(0);
        }
        this.total_score = 0;
        this.hero = new Plane(0);
        this.role_contain.addChild(this.hero);

        this.hero.pos(Main.s_width / 2, Math.floor(Main.s_height * 2 / 3));
        Laya.stage.on(Laya.Event.MOUSE_MOVE, this, this.onMouseMove);
        Laya.timer.frameLoop(1, this, this.onLoop);
    }

    //主角随鼠标移动
    onMouseMove():void {
        if(Main.is_playing) {
            //鼠标在主角上时才能控制飞机
            if(Math.abs(this.hero.x -Laya.stage.mouseX) < this.hero.radius && Math.abs(this.hero.y -Laya.stage.mouseY) < this.hero.radius) {
                this.hero.pos(Laya.stage.mouseX, Laya.stage.mouseY);
            }
        }
    }

    //提升游戏等级
    addGameLavel(_num: number = 1):void {
        this.game_level += _num;
        this.game_panel.setGameLevel(GameMain.game_level);
    }
    //主循环
    onLoop():void {
        if(Main.is_playing) {
            //敌机数量随等级增加
            if(Laya.timer.currFrame % Math.max(20, 60 - this.game_level) == 0 ){
                this.createEnemy();
            }
            //检测碰撞
            let count_stagerole: number = this.role_contain.numChildren;
            let t_now: number = Laya.Browser.now();
            for(let flyrole_i: number = 0; flyrole_i < count_stagerole; flyrole_i ++ ) {
                let flyrole1: Flyrole = this.role_contain.getChildAt(flyrole_i) as Flyrole;
                if(!flyrole1) continue;
                //前进和发射子弹
                flyrole1.flyForward();
                if(flyrole1 instanceof Plane && t_now > flyrole1.shootTime) {
                    flyrole1.shoot();
                }else if(flyrole1 instanceof Props && flyrole1.is_exixt){
                    //道具显示限制时间
                    let t_time_int: number = flyrole1.disappear_time - t_now;
                    if(t_time_int <= 0) {
                        flyrole1.rollbackSelf();
                    }else if(t_time_int < 1600) {
                        flyrole1.visible = !flyrole1.visible;
                    }
                }
                //与另一个物体的碰撞
                for(let flyrole_j: number = flyrole_i + 1; flyrole_j < count_stagerole; flyrole_j ++ ) {
                    let flyrole2: Flyrole = this.role_contain.getChildAt(flyrole_j) as Flyrole;
                    if(!flyrole2) continue;
                    let total_radius = flyrole1.radius + flyrole2.radius;
                    if(flyrole1.is_exixt && flyrole2.is_exixt && flyrole1.camp !== flyrole2.camp && !(flyrole1 instanceof Bullet && flyrole2 instanceof Bullet) && Math.abs(flyrole1.x - flyrole2.x) < total_radius && Math.abs(flyrole1.y - flyrole2.y) < total_radius) {
                        if(!(flyrole1 instanceof Props)) {
                            flyrole2.attacked();
                        }else if(flyrole2 instanceof Plane){
                            flyrole2.getBuff(flyrole1.type);
                        }
                        if(!(flyrole2 instanceof Props)) {
                            flyrole1.attacked();
                        }else if(flyrole1 instanceof Plane){
                            flyrole1.getBuff(flyrole2.type);
                        }
                    }
                }
            }
        }
    }

    //产生敌人
    createEnemy(_num: number = 1):void {
        for(let enemy_index: number=0; enemy_index < _num; enemy_index++) {
            let random_num1:number = this.total_score % 50 == 0 ? 1 : Math.random();
            //根据等级设置各机型比例
            let t_side:any = this.game_level / 100;
            this.role_contain.addChild(
                Laya.Pool.getItemByClass("plane", Plane)
                    .init(random_num1 < Math.max(0.4, 0.7 - t_side) ? 1 : random_num1 < Math.max(0.8, 0.98 - t_side) ? 2 : this.total_score > 10 ? 3 : 2)
                    .pos(Math.min(Main.s_width * Math.random() + 40, 360), 0)
            );
        }
    }
}

let GameMain:Main = new Main();